package com.lambda.skin.base;

import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.iyoyi.skinlib.OnSkinChangedListener;
import com.iyoyi.skinlib.SkinManager;
import com.lambda.skin.R;

public abstract class BaseActivity extends AppCompatActivity implements OnSkinChangedListener {
    private boolean mRecreated;

    @LayoutRes
    protected abstract int getLayoutId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mRecreated) recreate();
    }

    @Override
    public Resources getResources() {
        return SkinManager.getResources();
    }

    @Override
    public void onSkinChanged(Resources res) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setBackgroundDrawable(ContextCompat.getDrawable(this, R.color.colorPrimary));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
        }

        try {
            res.getResourceName(getLayoutId());
            mRecreated = true;
        } catch (Exception e) {
            e.printStackTrace();
            mRecreated = false;
        }
    }
}
